package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.Company;
import com.zygh.policeasset.pojo.Unit;
import com.zygh.policeasset.service.CompanyService;
import com.zygh.policeasset.service.UnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/unit"})
@Api(tags = {"单位管理"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/UnitController.class */
public class UnitController {
    @Autowired
    UnitService unitService;
    @Autowired
    CompanyService companyService;

    @RequestMapping(value = {"/findall"}, method = {RequestMethod.GET})
    @ApiOperation("获取全部单位列表")
    public Object findAll(@RequestParam(value = "type", required = false) String type) {
        return this.unitService.findAll(type);
    }

    @RequestMapping(value = {"/listfindall"}, method = {RequestMethod.GET})
    @ApiOperation("获取全部单位分页列表")
    public Object getAllByRoot(@RequestParam String type, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        return this.unitService.listFindAll(type, pageNum, pageSize);
    }

    @RequestMapping(value = {"/findbytype"}, method = {RequestMethod.GET})
    @ApiImplicitParam(name = "type", value = "zn:智能 jc：基础 zd:建设单位 cg:采购科室", required = true, paramType = "query")
    @ApiOperation("分类获取单位列表")
    public Object findByType(@RequestParam String type) {
        return this.unitService.findByType(type);
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加单位")
    public Object add(@RequestBody Unit params) {
        System.out.println(params);
        int res;
        if (StrUtil.isEmpty(params.getType())) {
            return new CommonResult().validateFailed("单位类型不能为空");
        }
        if (StrUtil.isEmpty(params.getUnitNum())) {
            return new CommonResult().validateFailed("公司编号不能为空");
        }
        if (this.unitService.findUnitByTypeAndUnit(params.getType(), params.getUnitNum(), null).intValue() > 0) {
            return new CommonResult().validateFailed("该单位类型的编号已存在");
        }
        if (this.unitService.findUnitByTypeAndUnit(params.getType(), null, params.getUnitName()).intValue() > 0) {
            return new CommonResult().validateFailed("该类型单位名称已存在");
        }
        List<Company> list = this.companyService.findAllBycompanyName(params.getUnitName());
        if (list.size() > 0) {
            params.setCompanyId(list.get(0).getId());
            res = this.unitService.add(params).intValue();
        } else {
            Company company = new Company();
            company.setCompanyName(params.getUnitName());
            params.setCompanyId(this.companyService.addresId(company));
            res = this.unitService.add(params).intValue();
        }
        if (res > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("修改单位")
    public Object update(@RequestBody Unit params) {
        if (StrUtil.isEmpty(params.getType())) {
            return new CommonResult().validateFailed("单位类型不能为空");
        }
        if (StrUtil.isEmpty(params.getUnitNum())) {
            return new CommonResult().validateFailed("公司编号不能为空");
        }
        if (this.unitService.findUnitByTypeAndUnit(params.getType(), params.getUnitNum(), null).intValue() > 0) {
            return new CommonResult().validateFailed("该单位类型的编号已存在");
        }
        if (this.unitService.findUnitByTypeAndUnit(params.getType(), null, params.getUnitName()).intValue() > 0) {
            return new CommonResult().validateFailed("该类型单位名称已存在");
        }
        if (this.unitService.update(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改错误");
    }

    @RequestMapping(value = {"/remove"}, method = {RequestMethod.GET})
    @ApiOperation("删除单位")
    public Object remove(@RequestParam int id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.unitService.remove(id).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }
}